﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using AcWin = Autodesk.AutoCAD.Windows;
using WpfCtrl = System.Windows.Controls;

#if ACAD2012_OR_BELOW
using System.Windows.Forms;
using System.Windows.Forms.Integration;
#endif

namespace ObjectArxNet
{
    public class PaletteSet
    {
        public PaletteSet(string name)
        {
            this.Name = name;
            _paletteSet = new AcWin.PaletteSet(this.Name, Guid.NewGuid())
            {
                Style = AcWin.PaletteSetStyles.ShowPropertiesMenu |
                        AcWin.PaletteSetStyles.ShowAutoHideButton |
                        AcWin.PaletteSetStyles.ShowCloseButton,
                MinimumSize = new System.Drawing.Size(300, 300)
            };
        }

        public string Name { get; }

        public bool Visible
        {
            get => _paletteSet.Visible;
            set => _paletteSet.Visible = value;
        }

        public void AddPalette(string name, WpfCtrl.UserControl userControl)
        {
#if ACAD2012_OR_BELOW
            var host = new ElementHost()
            {
                AutoSize = true,
                Dock = DockStyle.Fill,
                Child = userControl
            };
            _paletteSet.Add(name, host);
#else
            _paletteSet.AddVisual(name, userControl);
#endif
        }

        private readonly AcWin.PaletteSet _paletteSet;
    }

    public class PaletteManager
    {
        public static PaletteManager Current { get; } = new PaletteManager();

        private PaletteManager() { }

        private readonly Dictionary<string, PaletteSet> _paletteSets = new Dictionary<string, PaletteSet>();

        public bool HasPaletteSet(string name) => _paletteSets.ContainsKey(name);

        public PaletteSet GetPaletteSet(string name)
        {
            Debug.Assert(HasPaletteSet(name));
            return _paletteSets[name];
        }

        public PaletteSet AddPaletteSet(string name)
        {
            if (HasPaletteSet(name))
                return GetPaletteSet(name);

            var ps = new PaletteSet(name);
            _paletteSets.Add(name, ps);
            return ps;
        }
    }
}
